<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="org.apache.tools.ant.taskdefs.Filter"%>
<%@page import="com.google.appengine.api.datastore.Query.FilterOperator"%>
<%@page import="java.util.logging.SimpleFormatter"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.util.*, java.text.*" %>
<%@ page import="com.google.appengine.api.users.*" %>
<%@ page import="com.google.appengine.api.datastore.*" %>
<%@ page import="com.teamcloud.common.util.*" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>    
<%
    UserService userService = UserServiceFactory.getUserService();
    User user = userService.getCurrentUser();
    
    String q = request.getParameter("q");
    if(q==null) q = "";
    
    //SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    
    //한페이징당 행갯수 처리
    int PAGE_SIZE = 10;
    
    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    Key groupKey = KeyFactory.createKey("teamcloud", "group");
    
    //그룹 목록을 등록일 순으로 정렬
   	Query query = new Query("group", groupKey);
    
    				//.addFilter("groupNm", operator, q)
   	query.addSort("frstRegistPnttm", Query.SortDirection.DESCENDING);
    
    PreparedQuery queryString = datastore.prepare(query);
    
    int rowCount = queryString.countEntities();	//전체 행 갯수
    
  	//set offset for the results to be fetched
    String off = request.getParameter("offset");
    int offset = 0;
    if (off != null)
    	offset = Integer.parseInt(off);

    //fetch results from datastore based on offset and page size
    FetchOptions fetchOptions = FetchOptions.Builder.withLimit(PAGE_SIZE);
    QueryResultList<Entity> results = queryString.asQueryResultList(fetchOptions.offset(offset));
%>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<meta http-equiv="X-UA-Compatible" content="chrome=1">
	<link href="http://www.gstatic.com/codesite/ph/17444577587916266307/css/ph_core.css" rel="stylesheet" type="text/css" />
	<link href="http://code.google.com/css/codesite.pack.04102009.css" rel="stylesheet" type="text/css" />
	<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
	<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.0/jquery-ui.min.js"></script>
	<title>TeamCloud - 그룹 목록</title>
	<script>
		
		//페이지 이동
		function fn_list(offset) {
			document.searchForm.offset.value = offset;
			document.searchForm.submit();
			
		}
	</script>
</head>
<body>
<!-- content -->
<div id="gc-pagecontent">
	<h1 class="page_title">TeamOffice</h1>
	
	<!-- tabs -->
	<!-- <div id="tabs" class="gtb">
		<a href="#" class="tab">Home</a> 
		<a href="#" class="tab active">이슈 관리</a> 
		<a href="#" class="tab">Product</a> 
		<a href="#" class="tab">Item</a> 
		<a href="#" class="tab">Order</a>
		<div class="gtbc"></div>
	</div> -->

	<!-- search container -->
   	<div class="gsc-search-box" id="customer-search-ctr">
	   	<!-- section title -->
	   	<h2>그룹 목록</h2>
	   	<form name="searchForm" id="searchForm" method="post">
	   		<input type="hidden" name="offset" value="0" />
	   		<label>키워드</label>
			<input type="text" name="q" id="q" value="<%=q%>" class="gsc-input"/> 
	   	    <input type="submit" value="검색" class="gsc-search-button" />
			<input type="button" value="등록" onclick="location.href='groupForm.jsp'" class="gsc-search-button" />
			<input type="reset" id="customer-search-reset" class="cancel" title="Reset" value="Reset" style="visibility: hidden"/>
	    </form>
    </div>
    
    <!-- list container -->
	<div class="results" style="border:0;" id="customer-list-ctr">
		<table width="700" cellspacing="0" cellpadding="0" border="0">
			<thead>
				<tr>
					<th scope="col" width="30">번호</th>
					<th scope="col" width="*">그룹명</th>
					<th scope="col" width="90">등록자</th>
					<th scope="col" width="65">등록일</th>
					<th scope="col" width="40">공개</th>
					<th scope="col" width="45">멤버수</th>
				</tr>
			</thead>
	        <tbody>
<%
  	//set the content of the table
    for (Entity entity : results) {
    	long groupId = entity.getKey().getId();
    	String groupNm = (String) entity.getProperty("groupNm");
    	User googleUser = (User) entity.getProperty("googleUser");
    	Date frstRegistPnttm = (Date)entity.getProperty("frstRegistPnttm");
    	String othbcAt = (String) entity.getProperty("othbcAt");
%>
				<tr>
					<td><%=groupId%></td>
					<td><a href='groupDetail.jsp?groupId=<%=groupId %>'><%=groupNm%></a></td>
					<td><%=googleUser.getNickname()%></td>
					<td><%=formatter.format(frstRegistPnttm)%></td>
					<td><%=othbcAt.equals("Y")?"공개":"비공개"%></td>
					<td>0</td>
				</tr>
<%
    }
    if (results.isEmpty()) {
   	 	// condition to show message when data is not available
%>   	 	
    	<tr><td colspan=6>No records found</td></tr>			
<% } %>	
            </tbody>
            <tfoot>
				<tr>
					<td colspan=6 class="id" style="text-align:right">
						<%=PagingUtil.out(request, rowCount, offset, "fn_list")%>
					</td>
				</tr>
			</tfoot>
		</table>
	</div>
</div>
</body>
</html>